﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Supermarket;
using System.Data.SqlClient;

namespace XLSupermarket.Cashier.Order
{
    public partial class Select_Order : Form
    {
        public Select_Order()
        {
            InitializeComponent();
        }

        private void BT_Sel_Click(object sender, EventArgs e)
        {
            SqlDataReader sdr = null;
            try
            {
                //清空listView，防止上次查询信息继续保存
                listView1.Items.Clear();
                String Order_Numer = TB_ON.Text;
                int count = 0;
                string sql = "select OD.Order_Number 订单编号, GW.Bar_code 条形码,Trade_Name 商品名称,Gw.Specifications 规格,MI.Retail_Price 单价,COUNT(GW.Bar_code) 数量,MI.Discount 折扣, MI.Retail_Price* MI.Discount 折扣价,MI.Retail_Price 小计,GW.DateOfManufacture 生产日期, GW.QualityGuaranteePeriod 保质期, DATEADD(DAY, GW.QualityGuaranteePeriod,GW.DateOfManufacture) 到期日期,SL.Supplier_Name 供应商名称, GW.Other 备注 ,SO.Time_Of_Sale 售出时间 from Goods_Warehousing GW,Supplier SL, Merchandise_inventory MI ,Order_Details OD,Sales_Order SO where GW.Bar_code = MI.Bar_code and SL.Supplier_ID = GW.Supplier_ID and SO.Order_Number=OD.Order_Number and OD.Bar_code=MI.Bar_code and OD.Order_Number='" + Order_Numer + "' GROUP BY GW.Bar_code ,Trade_Name,Gw.Specifications ,MI.Retail_Price  ,MI.Discount ,MI.Retail_Price* MI.Discount ,GW.DateOfManufacture ,GW.QualityGuaranteePeriod ,OD.Order_Number,SO.Time_Of_Sale,DATEADD(DAY, GW.QualityGuaranteePeriod, GW.DateOfManufacture) ,SL.Supplier_Name ,GW.Other ";
                sdr = DBHelper.Readers(sql);
                while (sdr.Read())
                {
                    string order_number = sdr[0].ToString();//订单编号
                    string bar_code = sdr[1].ToString();//条形码
                    string trade_name = sdr[2].ToString();//商品名称
                    string Specifications = sdr[3].ToString();//规格
                    string Retail_Price = sdr[4].ToString();//单价
                    string Number = sdr[5].ToString();//数量
                    string Discount = sdr[6].ToString();//折扣
                    string discount_price = sdr[7].ToString();//折扣价
                    string SumPrice = sdr[8].ToString();//小计
                    DateTime dt = DateTime.Parse(sdr[9].ToString());
                    string DateOfManufacture = dt.ToString("yyyy-MM-dd");//生产日期
                    string QualityGuaranteePeriod = sdr[10].ToString();//保质期
                    dt = DateTime.Parse(sdr[11].ToString());
                    string Due_date = dt.ToString("yyyy-MM-dd");//到期日期
                    string Supplier_Name = sdr[12].ToString();//供货商名称
                    string Other = sdr[13].ToString();//备注
                    dt = DateTime.Parse(sdr[14].ToString());
                    string date = dt.ToString("yyyy-MM-dd");//下单日期

                    ListViewItem item1 = new ListViewItem((++count).ToString());//商品序号
                    item1.SubItems.Add(order_number);//订单编号
                    item1.SubItems.Add(bar_code);//条形码
                    item1.SubItems.Add(trade_name);//商品名称
                    item1.SubItems.Add(Specifications);//规格
                    item1.SubItems.Add(Retail_Price);//单价
                    item1.SubItems.Add("1");//数量
                    item1.SubItems.Add(Discount);//折扣
                    item1.SubItems.Add(discount_price);//折扣价
                    item1.SubItems.Add(SumPrice);//小计
                    item1.SubItems.Add(DateOfManufacture);//生产日期
                    item1.SubItems.Add(QualityGuaranteePeriod);//保质期
                    item1.SubItems.Add(Due_date);//到期日期
                    item1.SubItems.Add(Supplier_Name);//供货商名称
                    item1.SubItems.Add(Other);//备注
                    item1.SubItems.Add(date);//下单日期
                    listView1.Items.Add(item1);
                }
                if (int.Parse(listView1.Items.Count.ToString())<=0)
                {
                    MessageBox.Show("暂无此订单编号内容！\n请检查订单编号是否输入正确！","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                }
            }
            finally {
                sdr.Close();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
